'''
Created on Jun 14, 2009

@author: William
'''

import pygame
import random
import math
import time
from pygame.locals import *
pygame.init()

clock=pygame.time.Clock()
screen = pygame.display.set_mode((640, 480))
pygame.key.set_repeat(50,50)
pygame.mouse.set_visible(True)
pygame.event.set_grab(False)
myFont=pygame.font.SysFont("arial",33)
running=1
pointing=1
die=0
shooting=0
bullets=[]

x=200
y=200
xx=200
yy=180
ex=400
ey=300
k=0


while running:
    event = pygame.event.poll ()
    if event.type == pygame.QUIT:
        running=0
    screen.fill((0, 0, 0))
    
    # SET UP THE INPUTS
    #pressed_mouse=pygame.mouse.get_pressed()
    xy=pygame.mouse.get_pos()
    pressed_keys = pygame.key.get_pressed()
    
    
    # DIRECTION BASED UPON MOUSE POS
    if xy[0]>x and abs(xy[1]-y)<=10:
        pointing=2
    elif xy[0]<x and abs(xy[1]-y)<=10:
        pointing=4
    elif abs(xy[0]-x)<=10 and xy[1]<y:
        pointing=1
    elif abs(xy[0]-x)<=10 and xy[1]>y:
        pointing=3
    
    # MOVEMENT BASED UPON KEYS
    mv=2
    if pressed_keys[K_SPACE]:
        shooting=1
    elif pressed_keys[K_a]:
        x=x-mv
    elif pressed_keys[K_d]:
        x=x+mv
    if pressed_keys[K_w]:
        y=y-mv
    elif pressed_keys[K_s]:
        y=y+mv
    
    # CHANGE POSITION OF POINTING
    if pointing==1:
        xx=x
        yy=y-20
    elif pointing==2:
        xx=x+20
        yy=y
    elif pointing==3:
        xx=x
        yy=y+20
    elif pointing==4:
        xx=x-20
        yy=y
    
    # CHANGE DIRECTOIN OF ENEMY
    if ex>x:
        ex=ex-1
    else:
        ex=ex+1
    if ey>y:
        ey=ey-1
    else:
        ey=ey+1

    
    # CREATE AND DRAW THE BULLETS , CHECK FOR COLLISIONS 
    if shooting==1:
        bullets.append([xx,yy,pointing,1])
        shooting=0
    for shot in bullets:
        bmove=3
        if shot[3]==1:
            pygame.draw.circle(screen,(255,255,0),(shot[0],shot[1]),(2))
        if shot[2]==1:
            shot[1]=shot[1]-bmove
        elif shot[2]==2:
            shot[0]=shot[0]+bmove
        elif shot[2]==3:
            shot[1]=shot[1]+bmove
        elif shot[2]==4:
            shot[0]=shot[0]-bmove
        if shot[0]<0 or shot[0]>640 or shot[1]<0 or shot[1]>480:
            shot[3]=0
        
        # SHOOT COLLISION, IN FOR LOOP TO CHECK EACH BULLET
        if abs(shot[0]-ex)<=3 and abs(shot[1]-ey)<=3:
            ex=random.randint(0,640)
            ey=random.randint(0,480)
            k=k+1
    
    # CHECK FOR COLLISION WITH PLAYER
    if abs(xx-ex)<=20 and abs(yy-ey)<=20:
        msg =myFont.render("You Died!",True,(255,0,255))
        screen.blit(msg,(100,300))
        running=0
            
    
    #  DRAW EVERYTHING
    pygame.draw.circle(screen,(0,222,0),(x,y),(20))
    pygame.draw.circle(screen,(255,0,0),(xx,yy),(3))
    pygame.draw.circle(screen,(200,100,10),(ex,ey),(10))
    msg =myFont.render("You've killed "+str(k),True,(111,222,111))
    screen.blit(msg,(10,30))
   

    clock.tick(30)
    pygame.display.update()
time.sleep(4)
pygame.quit ()